﻿using FPSO.Models.Constant;
using FPSO.Models.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FPSO.Services.Impl
{
    public class CalcDraughtService : IDraughtService
    {
        /// <summary>
        /// 根据舱室装载容量计算船体状态
        /// </summary>
        /// <param name="tankVolumeDic"></param>
        /// <returns></returns>
        public ShipDraught CalcDraught(Dictionary<string, double> tankVolumeDic)
        {
            SafeLoadAlgorithmService safeLoadService = SafeLoadAlgorithmService.GetInstance();
            //Dictionary<int, double> fullDic = new Dictionary<int, double>();
			//foreach(KeyValuePair<int, double> pair in tankVolumeDic)
			//{
			//    double maxVolume = TankConstant.GetTankVolume(pair.Key);
			//    double full = 0d;
			//    if (pair.Value <= 0) { full = 0d; }
			//    else if (pair.Value >= maxVolume) { full = 100d; }
			//    else { full = (pair.Value * 100) / maxVolume; }
			//    fullDic.Add(pair.Key, full);
			//}
			//ManualCalcResult calcResult = safeLoadService.CallManualCalc(fullDic);
			ManualCalcResult calcResult = safeLoadService.CallManualCalc(tankVolumeDic,3);
			ShipDraught draught = new ShipDraught();
			//DEBUG 这里临时这么处理
			draught.MidPDraft = calcResult.ShipCalc.TM + 45.2 * Math.Tan(calcResult.ShipCalc.Heel);
			draught.MidPDraft = calcResult.ShipCalc.TM - 45.2 * Math.Tan(calcResult.ShipCalc.Heel);
			draught.AftPDraft = calcResult.ShipCalc.TM + calcResult.ShipCalc.Trim / 2 + 45.2 * Math.Tan(calcResult.ShipCalc.Heel);
			draught.AftSDraft = calcResult.ShipCalc.TM + calcResult.ShipCalc.Trim / 2 - 45.2 * Math.Tan(calcResult.ShipCalc.Heel);
			draught.FwdPDraft = calcResult.ShipCalc.TM - calcResult.ShipCalc.Trim / 2 + 45.2 * Math.Tan(calcResult.ShipCalc.Heel);
			draught.FwdSDraft = calcResult.ShipCalc.TM - calcResult.ShipCalc.Trim / 2 - 45.2 * Math.Tan(calcResult.ShipCalc.Heel);
            draught.Heel = calcResult.ShipCalc.Heel;
            draught.Trim = calcResult.ShipCalc.Trim;
            return draught;
        }
    }
}
